//
//  UIBezierViewController.swift
//  swift-practice
//
//  Created by 余振涛 on 2021/7/23.
//

import UIKit

class UIBezierViewController: UIViewController {

    init(withTitle title:String){
        super.init(nibName: nil, bundle: nil)
        self.title = title
        self.view.backgroundColor = .white
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        drawPath()
        // Do any additional setup after loading the view.
    }
    
    func drawPath() {
        UIColor.yellow.setFill()
        UIColor.red.setStroke()
        
        let path1 = UIBezierPath.init()
        path1.move(to: CGPoint(x: 10, y: 80))
        path1.addLine(to: CGPoint(x: 80, y: 80))
        path1.lineWidth = 5
        path1.stroke()
        
        let path2 = UIBezierPath.init()
        path2.addArc(withCenter: CGPoint(x: 200, y: 200), radius: 100, startAngle: 1.0, endAngle: 180.0, clockwise: true)
        path2.stroke()
        path2.fill()
        
        let path3 = UIBezierPath.init(rect: CGRect(x: 10, y: 100, width: 70, height: 100))
        path3.stroke()
        path3.fill()
        
        let path4 = UIBezierPath.init(ovalIn: CGRect(x: 10, y: 210, width: 70, height: 100))
        path4.stroke()
        path4.fill()
        
        let path5 = UIBezierPath.init(arcCenter: CGPoint(x: 200, y: 400), radius: 50, startAngle: 80.0, endAngle: 0, clockwise: true)
        path5.stroke()
        
        let path6 = UIBezierPath.init()
        path6.move(to: CGPoint(x: 100, y: 400))
        path6.addArc(withCenter: CGPoint(x: 100, y: 400), radius: 50, startAngle: 20, endAngle: 0, clockwise: true)
        path6.fill()
        path6.stroke()
        
        let frontCircle = CAShapeLayer.init()
        let backCircle = CAShapeLayer.init()
        self.view.layer.addSublayer(backCircle)
        self.view.layer.addSublayer(frontCircle)
    
        let path7 = UIBezierPath.init(arcCenter: CGPoint(x: 250, y: 600), radius: 80, startAngle: 0, endAngle: CGFloat(Double.pi) * 2, clockwise: true )
        backCircle.lineWidth = 18
        backCircle.strokeColor = UIColor.gray.cgColor
        backCircle.fillColor = UIColor.clear.cgColor
        backCircle.lineCap = CAShapeLayerLineCap.round
        backCircle.path = path7.cgPath
        
        let path8 = UIBezierPath.init(arcCenter: CGPoint(x: 250, y: 600), radius: 80, startAngle: 0, endAngle: CGFloat(Double.pi) * 2, clockwise: true)
        frontCircle.lineWidth = 18
        frontCircle.strokeColor = UIColor.blue.cgColor
        frontCircle.fillColor = UIColor.clear.cgColor
        frontCircle.lineCap = CAShapeLayerLineCap.round
        frontCircle.path = path8.cgPath
        
        let animation = CABasicAnimation.init()
        animation.keyPath = "strokeEnd"
        animation.duration = 3
        animation.fromValue = [0]
        animation.toValue = [0.5]
        frontCircle.strokeEnd = 0.5
        frontCircle.add(animation, forKey: nil)
        
    }

}
